package com.wshl.aijiuunion.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.utils.Log;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.view.bean.EventBeans;

import org.greenrobot.eventbus.EventBus;

;

/**
 * Created by Administrator on 2018/5/30.
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = ".WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.pay_result);
        //注册微信appid
        api = WXAPIFactory.createWXAPI(this, null);
        api.registerApp(Constancts.WXAPPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            LogUtils.e("微信支付结果-----%s:" + String.valueOf(resp.errCode));
            if ("0".equals(String.valueOf(resp.errCode))) {        //0支付成功，-1取消支付，-2支付失败
                ToastUtils.showLong("微信支付成功");
                //自己处理支付成功后操作
                // Toast.makeText(this, "微信支付成功", Toast.LENGTH_SHORT).show();
                //支付成功后跳转到订单列表
                EventBus.getDefault().post(new EventBeans(GlobConstant.WXPAYRESULT, GlobConstant.WXPAYSUCCESS));
                // ActivityUtils.startActivity(new Intent(WXPayEntryActivity.this, OrderManagerActivity.class));
                WXPayEntryActivity.this.finish();
            } else if ("-2".equals(String.valueOf(resp.errCode))) {

                EventBus.getDefault().post(new EventBeans(GlobConstant.WXPAYRESULT, GlobConstant.WXPAYCANCEL));
                Toast.makeText(this, "取消支付", Toast.LENGTH_SHORT).show();

                WXPayEntryActivity.this.finish();
            } else {
                EventBus.getDefault().post(new EventBeans(GlobConstant.WXPAYRESULT, GlobConstant.WXPAYFAILED));
                Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
                WXPayEntryActivity.this.finish();
            }


        }
    }
}

